
package privatemessanger;
import java.util.Random;
import java.util.ArrayList;

/**
 * This class contains the information of a client.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class Client
{
    private String _name; // the client name.
    private String _type; // the type of the client.
    private int _ID; // the ID of the client.
    private String _password; // the password of the clien.
    private String _IP; // the IP of the client.
    private ArrayList<Server> _serverArray; //array of servers.
    private ArrayList<Message> _messageArray; // array of messages.

    /**
     * This constructor is for client who has been created in the registration.
     * @param name The client's name.
     * @param password The client's password.
     */
    public Client(String name, String password)
    {
        Random rand=new Random();
        _name=name;
        _type="General";
        _ID=rand.nextInt(899999)+100000;
        _password=password;
        try {
            java.net.InetAddress i = java.net.InetAddress.getLocalHost();
            _IP=(i.getHostAddress());
        } catch(Exception error){error.printStackTrace();}
        _serverArray=new ArrayList<Server>();
        _messageArray=new ArrayList<Message>();
    }

    /**
     * This constructor is for client who has been created in the log in.
     * All parameters are received from the database.
     * @param name The client's name.
     * @param type The client's type.
     * @param ID The client's ID.
     * @param password The client's password.
     * @param IP The client's IP.
     */
    public Client(String name, String type, int ID, String password, String IP)
    {
        _name=name;
        _type=type;
        _ID=ID;
        _password=password;
        try {
            java.net.InetAddress i = java.net.InetAddress.getLocalHost();
            _IP=(i.getHostAddress());
        } catch(Exception error){error.printStackTrace();}
        _serverArray=new ArrayList<Server>();
    }

    /**
     * This constructor is for client who has been connected to a real server.
     * All parameters received from the socket.
     * @param name The client's name.
     * @param type The client's type.
     * @param ID The client's ID.
     * @param IP The client's IP.
     */
    public Client(String name, String type, int ID, String IP)
    {
        _name=name;
        _type=type;
        _ID=ID;
        _IP=IP;
    }

    /**
     * Get The client's name.
     * @return The client's name.
     */
    public String getUserName() { return _name; }

    /**
     * Set the client's name.
     * @param name The client name.
     */
    public void setUserName(String name)
    {
        _name=name;
        // TODO update the database.
    }

    /**
     * Get The client's type.
     * @return The client's type.
     */
    public String getType() { return _type; }

    /**
     * Set The client's type.
     * @param type The client's type.
     */
    public void setType(String type)
    {
        _type=type;
        // TOOD update the database.
    }

    /**
     * Get The client's ID.
     * @return The client's ID.
     */
    public int getID() { return _ID; }

    /**
     * Get The client's IP.
     * @return The client's IP.
     */
    public String getIP() { return _IP; }

    /**
     * Get the servers array.
     * @return The servers array.
     */
    public ArrayList<Server> getServerArray() { return _serverArray; }

    /**
     * Get the messages array.
     * @return The messages array.
     */
    public ArrayList<Message> getMessageArray() { return _messageArray; }

    /**
     * This method checks The client's password.
     * @param password A password to check.
     * @return If the password is the same as the client's password- true, else false.
     */
    public boolean isPasswordCorrect(String password)
    {
        if (_password.equals(password))
            return true;
        return false;
    }

    /**
     * This method changes the password, but only if the old one is correct.
     * @param oldPassword The client's old password.
     * @param newPassword The client's new password.
     * @return if the password has been changed- return 0, else 1.
     */
    public int changePassword(String oldPassword, String newPassword)
    {
        if (isPasswordCorrect(oldPassword))
        {
            _password=newPassword;
            return 0; // password has been changed.
        }
        return 1; // password has not been changed.
    }

    /**
     * This method adds a new server to the servers array.
     * @param server An instance of a server.
     */
    public void addServer(Server server) { _serverArray.add(server); }

    /**
     * This method removes a server from the servers array.
     * @param server An instance of a server.
     */
    public void removeServer(Server server) { _serverArray.remove(server); }

    /**
     * This method removes a server from the servers array.
     * @param IP The server's IP.
     */
    public void removeServer(String IP)
    {
        for (int i=0; i<_serverArray.size(); i++)
        {
            if (_serverArray.get(i).getIP().equals(IP))
                _serverArray.remove(i);
        }
    }
}
